package com.snmp.smn.utils;

import java.util.Date;

import com.snmp.smn.pdu.GetRequestPDU;
import com.snmp.smn.pdu.PDUFactory;
import com.snmp.smn.pdu.SetRequestPDU;
import com.snmp.smn.pdu.TrapPDU;

public class Utilities {
	
	public static String getUserName(String message) {
		return message.substring(2);
	}

	public static boolean isCorrectVersion(String message) {
		return message.charAt(0) == PDUFactory.SNMP_VERSION.charAt(0);
	}
	
	public static String getMessageType(String message) {
		return "" + message.charAt(1);
	}

	public static String getMessageData(String message) {
		return message.substring(2);
	}

	public static boolean isGetRequest(String message) {
		return getMessageType(message).equalsIgnoreCase(GetRequestPDU.PDU_TYPE);
	}

	public static boolean isHeartBeat(String message) {
		return getMessageData(message).equalsIgnoreCase(GetRequestPDU.HEARTBEAT);
	}

	public static boolean isTrapMessage(String message) {
		return getMessageType(message).equalsIgnoreCase(TrapPDU.PDU_TYPE);
	}

	public static boolean isImproperShutdown(String message) {
		return getMessageData(message).equalsIgnoreCase("IS");
	}

	public static boolean isSetRequest(String message) {
		return getMessageType(message).equalsIgnoreCase(SetRequestPDU.PDU_TYPE);
	}

	public static boolean isGetFileInfo(String message) {
		return getMessageData(message).startsWith("FI");
	}

	public static boolean isGetFileContent(String message) {
		return getMessageData(message).startsWith("FC");	
	}

	public static String getFileName(String message) {
		String data = getMessageData(message);
		if (message.indexOf("|") == -1) {
			return data.substring(2);
		} else {
			return data.substring(2, data.indexOf("|"));
		}
	}

	public static int getIndex(String message) {
		String data = getMessageData(message);
		return Integer.parseInt(data.substring(data.indexOf("|") + 1));
	}
	
	public static boolean isErrorMessage(String message) {
		return message.startsWith("Error");
	}
	
	public static void log(String message) {
		System.out.println(new Date() + " : " + message);
	}
}
